import 'package:app/pages/daily_work_report_module/my_daily_detail_module/my_daily_detail_controller.dart';
import 'package:app/pages/userinfo_module/userinfo_view.dart';
import 'package:extended_image/extended_image.dart';
import 'package:extended_text/extended_text.dart';
import 'package:extended_text_field/extended_text_field.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../common/app_theme.dart';
import '../../../common/colors.dart';
import '../../../common/tool.dart';
import '../../../components/base_scaffold_appbar.dart';
import '../../../components/comment_cell.dart';
import '../../../components/nodata_page.dart';
import '../../../services/address.dart';
import '../../../utils/search_module.dart';
import 'package:flutter_svg/flutter_svg.dart';

import '../../personnel_module/personnel_card_page.dart';

class MyDailyDetailView extends GetView{

  MyDailyDetailController controller = Get.put(MyDailyDetailController());

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Colors.white,
      appBar:BaseAppBar(preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
      child: const Text('日报详情'),),
      body: SafeArea(
        bottom: false,
        child: GetBuilder<MyDailyDetailController>(builder: (_){
          return controller.model==null?const Center(
            child: CupertinoActivityIndicator(),
          ):Column(
            children: [
              Expanded(
                child: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    /// 页面点击响应
                    SearchModule.closeKeyboard(context);
                  },
                  onPanDown: (_) {
                    /// 列表滑动响应
                    SearchModule.closeKeyboard(context);
                  },
                  child: CustomScrollView(
                    slivers: [
                      SliverToBoxAdapter(
                        child: Container(
                          // margin: EdgeInsets.all(5),
                          // height: 10,
                          margin: EdgeInsets.only(top: 5),
                          padding: EdgeInsets.only(left: 15),
                          color: Colors.white,
                          child: Row(
                            children: [
                              CircleAvatar(
                                // radius: 15,
                                backgroundImage: NetworkImage(
                                    '${Address.homeHost}${controller.model?.data?.headerImg}'
                                ),

                              ),
                              Container(
                                margin: EdgeInsets.only(
                                  left: 5,
                                ),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Container(
                                      margin: EdgeInsets.only(top: 10),
                                      color: Colors.white,
                                      width: Get.width - 80,
                                      child: Row(
                                        mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text(
                                            '${controller.model?.data?.author}',
                                            style: appThemeData.textTheme.bodyText1,
                                          ),
                                          Text(
                                            '${controller.model?.data?.createDate}',
                                            style: appThemeData.textTheme.bodyText1!
                                                .copyWith(
                                                color: AppColor.themeColor,
                                                fontSize: 10),
                                          )
                                        ],
                                      ),
                                    ),
                                    SizedBox(
                                      height: 3,
                                    ),
                                    Text(
                                      '${controller.model?.data?.teamName}',
                                      style: appThemeData.textTheme.subtitle1
                                          ?.copyWith(fontSize: 11),
                                    ),
                                    SizedBox(
                                      height: 3,
                                    ),
                                    Text(
                                      '${controller.model?.data?.dateBeauty}',
                                      style: appThemeData.textTheme.subtitle1
                                          ?.copyWith(fontSize: 11),
                                    ),
                                    SizedBox(
                                      height: 3,
                                    ),
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                      SliverToBoxAdapter(
                        child: Container(
                          color: Colors.white,
                          // height: 200,
                          // constraints: BoxConstraints(
                          //   maxHeight: 100,
                          //   minHeight: 50
                          // ),
                          padding: EdgeInsets.only(left: 15, right: 15, top: 10),
                          child:ExtendedText(
                             '${controller.editingController.text}',
                              specialTextSpanBuilder: controller.mySpecialTextSpanBuilder,
                            onSpecialTextTap: (value){
                              print(value);
                               if(value.toString().startsWith('@')){
                                 print(value);
                                 Get.to(PersonnelCardPage(value));
                               }
                            },
                          ),

                          // ExtendedTextField(
                          //   readOnly: true,
                          //   style: TextStyle(fontSize: 14),
                          //   decoration: InputDecoration(
                          //     border: InputBorder.none,
                          //   ),
                          //   specialTextSpanBuilder: controller.mySpecialTextSpanBuilder,
                          //   controller: controller.editingController,
                          //   maxLines: 10,
                          //   minLines: 1,
                          // ),

                        ),
                      ),
                      SliverToBoxAdapter(
                        child: Container(
                          margin: EdgeInsets.only(left: 15, top: 10),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '附件:',
                                style: appThemeData.textTheme.subtitle1,
                              ),
                              Container(
                                // height: 200,
                                width: Get.width - 60,
                                child: ListView.builder(
                                  physics: const NeverScrollableScrollPhysics(),
                                  shrinkWrap: true,
                                  itemBuilder: (BuildContext context, int index) {
                                    return GestureDetector(
                                      onTap: ()async{
                                        await launch('${controller.fileModel?.data?[index].url}', forceSafariVC: false, forceWebView: false,);

                                        // Get.to(MyWebView('', _fileModel?.data?[index].url));
                                      },
                                      child: Container(
                                        padding:
                                        EdgeInsets.only(top: index != 0 ? 10 : 0),
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,

                                          children: [
                                            Row(
                                              crossAxisAlignment: CrossAxisAlignment.start,
                                              children: [
                                                SvgPicture.network('${Address.homeHost}${controller.fileModel?.data?[index].icon}',width: 15,height: 15,),
                                                Container(
                                                  width: Get.width-100,
                                                  child: Text('${controller.fileModel?.data?[index].name}'),
                                                ),
                                              ],
                                            ),
                                            GestureDetector(
                                              onTap: (){
                                                Get.bottomSheet(BottomSheet(onClosing: (){

                                                }, builder: (BuildContext context) {
                                                  return SafeArea(child: Container(
                                                    height: 100,
                                                    color: Colors.white,
                                                    child:Column(
                                                      children: [
                                                        Container(
                                                          color: Colors.transparent,
                                                          alignment: Alignment.center,
                                                          height: 50,
                                                          child:GestureDetector(
                                                            onTap: ()async{
                                                              Get.back();
                                                              await launch('${controller.fileModel?.data?[index].url}', forceSafariVC: false, forceWebView: false,);
                                                            },
                                                            child: Center(
                                                              child: Text('下载'),
                                                            ),
                                                          ),
                                                        ),
                                                        Container(
                                                          color: Colors.transparent,
                                                          height: 40,
                                                          child: GestureDetector(
                                                            onTap: (){
                                                              Get.back();
                                                              controller.tapClicKUnlink(index);
                                                              // controller.requestDataWithUnlink(Get.arguments,controller.fileModel?.data?[index].checksum);
                                                            },
                                                            child:  Center(
                                                              child: Text('删除'),
                                                            ),
                                                          ),
                                                        )
                                                      ],
                                                    ),
                                                  )
                                                  );
                                                },));
                                              },
                                              child: Image.asset('images/more_icon.png',width: 18,height: 18,),
                                            )
                                          ],
                                        ),
                                      ),
                                    );
                                  },
                                  itemCount: controller.fileModel?.data?.length ?? 0,
                                ),
                              )
                              // Image.network('')
                            ],
                          ),
                        ),
                      ),
                      SliverToBoxAdapter(
                        child: Container(
                          // height: 50,
                          padding: EdgeInsets.only(left: 15, right: 15, top: 10),
                          width: Get.width,
                          color: Colors.white,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '${controller.model?.data?.dateBeauty}',
                                style: appThemeData.textTheme.subtitle1
                                    ?.copyWith(fontSize: 11),
                              ),
                              GestureDetector(
                                onTap: () {
                                  print('111');
                                  controller.requestDataWithLike();
                                },
                                child: Image.asset(
                                  'images/like_icon.png',
                                  width: 15,
                                  height: 15,
                                  color:controller.model?.data?.is_zan==false?AppColor.smallTextColor:
                                  Colors.red,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      SliverToBoxAdapter(
                        child: Container(
                          margin: EdgeInsets.only(top: 5),
                          height: 10,
                          width: Get.width,
                          color: AppColor.bgColor,
                        ),
                      ),

                      SliverToBoxAdapter(
                        child: Container(
                            alignment: Alignment.centerLeft,
                            width: Get.width,
                            height: 37,
                            color: Colors.white,
                            padding: EdgeInsets.only(left: 15),
                            child: Column(
                              // mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                SizedBox(height: 10,),
                                Text('评论(${controller.model?.data?.comments})',style: appThemeData.textTheme.subtitle1!.copyWith(color: AppColor.themeColor),),
                                SizedBox(height: 5,),
                                Container(
                                  margin: EdgeInsets.only(top: 3),
                                  color: AppColor.themeColor,
                                  width: 20,
                                  height: 1,
                                )
                              ],
                            )
                        ),
                      ),
                      SliverToBoxAdapter(
                        child: Container(

                          width: Get.width,
                          height: 10,
                          color: AppColor.bgColor,
                        ),
                      ),
                      controller.commentModel?.data==null? SliverToBoxAdapter(
                        child: Container(
                          color: Colors.red,
                          width: Get.width,
                          height: 200,
                          child: NoDataPage(
                            text: '暂无评论',
                          ),
                        ),
                      ):
                      SliverList(
                        delegate: _mySliverChildBuilderDelegate(),
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                alignment: Alignment.topCenter,
                height: 45+MediaQuery.of(context).padding.bottom,
                color: Colors.grey.shade200,
                padding: EdgeInsets.only(bottom: 0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  // crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      width: Get.width - 100,
                      height: 35,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(20)),
                      ),
                      padding: EdgeInsets.only(left: 10),
                      alignment: Alignment.center,
                      child: TextField(
                        controller: controller.commentTextEditing,
                        decoration: InputDecoration(
                            isCollapsed: true,
                            border: InputBorder.none,
                            hintText: '请输入评论内容'),
                      ),
                    ),
                    IconButton(
                        onPressed: () {

                          controller.tapClickSendComment();
                          // if (_commentTextEditing.text.isEmpty) {
                          //   BotToast.showText(text: '评论不能为空');
                          //   return;
                          // }
                          // requestDataWithMessageAdd(_model?.data?.id);
                          // requestDataWithComment();
                          // requestData(id: widget.id);
                          // _commentTextEditing.text = '';
                          // SearchModule.closeKeyboard(context);
                        },
                        icon: Icon(
                          Icons.send,
                          color: AppColor.smallTextColor,
                        )),
                  ],
                ),
              )
            ],
          );
        }),
      ),
    );
  }

  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
        return CommentView(controller,index);
      },
      childCount: controller.commentModel?.data?.length,
    );
  }


}